<?php
namespace app\common\middleware;

use app\admin\model\Manager;
use app\lib\exception\BaseException;

class hasManagerLogin
{
    public function handle($request, \Closure $next)
    {
        $model = 'app\\admin\\model\\Manager';
       //获取头部信息
        $param = $request->header();
        //不含token
        if(!array_key_exists('token',$param)) TApiException('非法token,禁止操作',20003,200);
        //当前用户的token是否存在
        $token = $param['token'];
        $user = \Cache::get($token);
        //验证失败(为登录或者以过期)
        if(!$user) TApiException('非法token，请先登录',20003,200);
//        // 当前用户实例
        $request->UserModel =Manager::find($user['id']);
        // 当前用户已被禁用
        if(!$user['status']){
            return ApiException('当前用户已被禁用');
        }
        $request->id = $user['id'];
        // 当前用户数据
        $request->userInfo = $user;
        return $next($request);
    }
}